[解決済み] scipy sparse csr |
您所在的位置:网站首页 › scipy sparse秩 › [解決済み] scipy sparse csr |
質問
scipy sparseの保存/読み込みはどのように行うのですか? csr_matrix をポータブルなフォーマットで保存できますか? scipyのスパース行列は、Python 2 (Linux 64-bit)で実行するためにPython 3 (Windows 64-bit)で作成されています。 最初は pickle (protocol=2, fix_imports=True) を使いましたが、Python 3.2.2 (Windows 64-bit) から Python 2.7.2 (Windows 32-bit) に行くとうまくいかず、エラーになりました。 TypeError: ('data type not understood', , (, (0,), '[98]')).次に numpy.save と numpy.load と同様に scipy.io.mmwrite() と scipy.io.mmread() といった方法で、いずれもうまくいきませんでした。 どのように解決するのですか?を編集してください。 scipy 0.19では scipy.sparse.save_npz と scipy.sparse.load_npz . from scipy import sparse sparse.save_npz("yourmatrix.npz", your_matrix) your_matrix_back = sparse.load_npz("yourmatrix.npz")どちらの関数も file の引数は、ファイルのようなオブジェクトであることもあります。 open の結果) を指定することもできます。 Scipyユーザーグループから回答がありました。 csr_matrixは3つのデータ属性を持っています。 .data , .indices そして .indptr . すべて単純な ndarray です. numpy.save はそれらに対して動作します。3つの配列の保存には numpy.save または numpy.savez で読み込み、それを numpy.load で、疎行列オブジェクトを再作成します。 new_csr = csr_matrix((data, indices, indptr), shape=(M, N))だから例えば def save_sparse_csr(filename, array): np.savez(filename, data=array.data, indices=array.indices, indptr=array.indptr, shape=array.shape) def load_sparse_csr(filename): loader = np.load(filename) return csr_matrix((loader['data'], loader['indices'], loader['indptr']), shape=loader['shape']) |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |